<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>借书管理</title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
		<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
	</head>
	<body class="pear-container">
	<!--搜索组件-->
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">书名</label>
							<div class="layui-input-inline">
								<input type="text" name="bookname" id="select_bookname" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">ISBN</label>
							<div class="layui-input-inline">
								<input type="text" name="isbn" id="select_isbn" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">作者</label>
							<div class="layui-input-inline">
								<input type="text" name="author" id="select_author" placeholder="" class="layui-input">
							</div>
						</div>

<!--						<div class="layui-form-item layui-inline">-->
<!--							<label class="layui-form-label">状态</label>-->
<!--							<div class="layui-input-inline">-->
<!--								<select  name="statistic" id="select_statistic" placeholder="" class="layui-input">-->
<!--									<option value="0">有库存</option>-->
<!--									<option value="1">已借出</option>-->
<!--								</select>-->
<!--							</div>-->
<!--						</div>-->

						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="book-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	<!--信息显示表格组件-->
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="book-table" lay-filter="book-table"></table>
			</div>
		</div>
	<!--批量借书组件-->
		<script type="text/html" id="user-toolbar">
		    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="batchRemove"><i class="layui-icon layui-btn-radius"></i>批量借书</button>
		</script>
<!--借书按钮组件-->
		<script type="text/html" id="user-bar">
			 <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-btn-radius"></i>借书</button>
		</script>
<!--时间格式化显示组件-->
		<script type="text/html" id="user-createTime">
			{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
		</script>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;
				let MODULE_PATH = "operate/";

				// bookId, bookname, isbn, press, categoryId, author, vailbookId, number, surplus, lend, id, lend, lendTime, sendTime, bookid, user_id
				let cols = [
					[
							{
							type: 'checkbox'
						},
						{
							title: '书id',
							field: 'bookid',
							align: 'center',
							// templet: '<div>{{d.book.bookid}}</div>'
						},
						{
							title: '书名',
							field: 'bookname',
							align: 'center',
							// templet: '<div>{{d.book.bookname}}</div>'
						},
						{
							title: 'ISBN',
							field: 'isbn',
							align: 'center',
							// templet: '<div>{{d.book.isbn}}</div>'
						},
						{
							title: '出版社',
							field: 'press',
							align: 'center',
							// templet: '<div>{{d.book.press}}</div>'
						},
						{
							title: '类别',
							field: 'categoryId',
							align: 'center',
							templet:leibie
						},
						{
							title: '作者',
							field: 'author',
							align: 'center',
							// templet: '<div>{{d.book.author}}</div>'
						},
						{
							title: '位置信息',
							field: 'vailbookId',
                            align: 'center',
							templet: place
						},
						{
							title: '总数量',
							field: 'number',
							align: 'center',
							sort: true,
							// templet: '<div>{{d.book.number}}</div>'
						},
						{
							title: '剩余数量',
							field: 'surplus',
							align: 'center',
							// templet: '<div>{{d.book.surplus}}</div>'
						},
						{
							title: '操作',
							toolbar: '#user-bar',
                            align: 'center'
						}
					]
				]



				function leibie(d) {
					let str;
					if (d.categoryId == 1) {
						str = '计算机类';
					} else if (d.categoryId == 2) {
						str = '文史类';
					} else if (d.categoryId == 3) {
						str = '社科类';
					}
					return str;
				}

				function place(d) {
					let str;
					if (d.vailbookId == 1) {
						str = '2馆1号架';
					} else if (d.vailbookId == 2) {
						str = '2馆2号架';
					} else if (d.vailbookId == 3) {
						str = '3馆3号架';
					}
					return str;
				}
				table.render({
					elem: '#book-table',
					url: 'http://47.93.226.229/getAllBook',
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#user-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});

				table.on('tool(book-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(book-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(book-query)', function (data) {
					data.field.page = 1;
					data.field.limit = 5;
					$.ajax({
						url: 'http://47.93.226.229/getBorrowBook',
						data: data.field,//data，规定要发送服务器的数据
						type: "GET",//规定请求的类型
						dataType: "json",//预期服务器相应的类型
						success: function (a) {
							table.reload('book-table', {
								where: {
									bookname: $("#select_bookname").val(),
									isbn: $("#select_isbn").val(),
									author: $("#select_author").val(),
								}, page: {
									curr: 1 //重新从第 1 页开始
								}
							})
						}
					});
					return false;
				});

				window.remove = function(obj) {
					let data = obj.data; //获得当前行数据
					let arr = new Array();
					arr.push(data.bookid);
					if (obj.data.surplus<=0){
						layer.alert('剩余数量不够了！', {
							icon: 5,
							title: "提示"
						});
						return ;
					}
					layer.confirm('确定要借该书？', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: 'http://47.93.226.229/BorrowBookWithIds',
							data: {"ids": arr,"user1":JSON.parse(localStorage.getItem("user1")).id},
							dataType: 'json',
							traditional: true,
							type: 'POST',
							success: function (result) {
								layer.close(loading);
								if (result.success) {
									layer.msg(result.msg, {
										icon: 1,
										time: 10
									}, function () {
										// obj.del();
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 10
									});
								}
							}
						})
						table.reload("book-table",{
							done: function (res, curr, count) {
								if (curr > 1 && res.data.length === 0) {
									curr = curr - 1;
									table.reload('user-table', {
										page: {
											curr: curr
										},
										where: {
											bookname: $("#select_bookname").val(),
											isbn: $("#select_isbn").val(),
											author: $("#select_author").val(),
										},
									}, 'data');
								}
							}
						});
					});
				}

				window.batchRemove = function(obj) {
					// let checkIds = common.checkField(obj,'userId');
					let checkStatus = table.checkStatus(obj.config.id)
							, data = checkStatus.data; //获取选中的数据
					let arr = new Array();
					for (let i = 0; i < checkStatus.data.length; i++) {
						arr.push(checkStatus.data[i].bookid);
					}
					if (data.length === 0) {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}

					layer.confirm('确定要借这些书', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: 'http://47.93.226.229/BorrowBookWithIds',
							data:{"ids":arr,"user1":JSON.parse(localStorage.getItem("user1")).id},
							dataType: 'json',
							traditional:true,
							type:"POST",
							success: function(result) {
								layer.close(loading);
								if (result.success) {
									layer.msg(result.msg, {
										icon: 1,
										time: 10
									}, function() {
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 10
									});
								}
							}
						})
						table.reload('book-table');
					});
				}

				window.refresh = function(param) {
					table.reload('book-table');
				}
			})
		</script>
	</body>
</html>
